/*******************************************************************************
*
* (c) Copyright 2014 Freescale Semiconductor
*
****************************************************************************//*!
*
* @file     Accelerometer.c
*
* @author   Salvador Zendejas
*
* @version  0.0.1
*
* @date      Nov 15, 2014
*
* @brief    Accelerometer driver for MQX RTOS
*
*******************************************************************************/

#ifndef ACCELEROMETER_H_
#define ACCELEROMETER_H_

#include "mqx.h"	/* Include MQX declarations */
#include "bsp.h"

#if !BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif


#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif

#if defined BSP_VYBRID_AUTOEVB_A5 || defined BSP_VYBRID_AUTOEVB_M4
#define I2C_DEVICE_POLLED    "i2c1:"
#define I2C_DEVICE_INTERRUPT "ii2c1:"
#define ENABLE_I2C_INTERRUPT  BSPCFG_ENABLE_II2C1
#if ! BSPCFG_ENABLE_I2C1
#error This application requires BSPCFG_ENABLE_I2C1 defined non-zero in user_config.h. Please recompile BSP with this option.
#endif

#elif defined BSP_TWRPXS30
#define I2C_DEVICE_POLLED    "i2c2:"
#define I2C_DEVICE_INTERRUPT "ii2c2:"
#define ENABLE_I2C_INTERRUPT  BSPCFG_ENABLE_II2C2
#if ! BSPCFG_ENABLE_I2C2
#error This application requires BSPCFG_ENABLE_I2C2 defined non-zero in user_config.h. Please recompile BSP with this option.
#endif

#else
#define I2C_DEVICE_POLLED    "i2c0:"
#define I2C_DEVICE_INTERRUPT "ii2c0:"
#define ENABLE_I2C_INTERRUPT  BSPCFG_ENABLE_II2C0
#if ! BSPCFG_ENABLE_I2C0
#error This application requires BSPCFG_ENABLE_I2C0 defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#endif

#define	FXOS8700CQ			1
#define Accelerometer		FXOS8700CQ
#define	BYTE				1
#define	WORD				2
#define CONTINOUS_DATA		1

/*Defines for FXOS8700CQ*-------------------------------------------------------------------
* 
* Device : main_task
* Comments  : Xtrinsic FXOS8700CQ 6-Axis
* Sensor with Integrated Linear
* Accelerometer and Magnetometer
*
*END*----------------------------------------------------------------------*/
#if defined Accelerometer == FXOS8700CQ

#if defined BSP_FRD_K64F
	#define SLAVE_ADDRESS	0x1D
#endif

#define DATA_RESOLUTION	0x0E	//14Bits (default)

#if DATA_RESOLUTION == 0x08
	#define DATA_LENGHT		0x02
#else
	#define DATA_LENGHT		0x01
#endif

#define STATUS_REG		0x00
#define TRIG_CFG		0x0A
#define CTRL_REG1		0x2A
#define CTRL_REG2		0x2B
#define CTRL_REG3		0x2C
#define CTRL_REG4		0x2D
#define CTRL_REG5		0x2E
#define WHO_AM_I		0x0D
#define WHO_AM_I_VAL	0xC7

#define X_MSB_REG		0x01
#define X_LSB_REG		0x02
#define Y_MSB_REG		0x03
#define Y_LSB_REG		0x04
#define Z_MSB_REG		0x05
#define Z_LSB_REG		0x06
#define XYZ_DATA_CFG	0x0E
#define	MODE_2G			0x00
#define	MODE_4G			0x01
#define	MODE_8G			0x02
#define SEN_2G			244
#define SEN_4G			488
#define SEN_8G			976
#define G_MODE			MODE_4G
#if (G_MODE == MODE_2G)
	#define SENSIVITY		SEN_2G
#elif (G_MODE == MODE_4G)
	#define SENSIVITY		SEN_4G
#else
	#define SENSIVITY		SEN_8G
#endif /*G_MODE if */
#endif /*Accelerometer == FXOS8700CQ if*/

/* 
** IOCTL calls specific to I2C 
*/
#define IO_IOCTL_ACCELEROMETER_SET_G_MODE		0x01
#define IO_IOCTL_ACCELEROMETER_GET_G_MODE		0x02

/* 
** I2C Error Codes
*/
#define ACCEL_OK                               (0x00)
#define ACCEL_ERROR                            (0x01)
/*******************************************************************************
* MACROS
*******************************************************************************/
#define ACCELEROMETER_INSTALL	_io_accelerometer_install()
#define TO_G_UNITS(val)			(val/100)
#define TO_G_DECIMALS(val)		(val%100)
#define TABS					"\t\t ... \t ... "
/*******************************************************************************
* Types Definitions
*******************************************************************************/
typedef enum {
	Mode_2g	= 0x00,
	Mode_4g,	//2b01
	Mode_8g,	//2b10
} eModeG_t;

extern MQX_FILE_PTR fd_accel;
uint8_t addr_reg[2];		// Keeps address to write/read the accelerometer device

_mqx_int _io_accelerometer_install(void);
_mqx_int _io_accelerometer_open(void);
_mqx_int _io_accelerometer_ioctl(_mqx_uint, void *);
void rx_data(void);

#endif /* ACCELEROMETER_H_ */
